from typing import List


class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        m = len(nums1)
        n = len(nums2)
        if m > n:
            nums1, nums2, m, n = nums2, nums1, n, m

        i, j = 0, 0
        left, right = 0, m
        while left <= right:
            i = (left + right) // 2
            j = (m + n + 1) // 2 - i
            if (nums1[i - 1] if i != 0 else float("-inf")) > (
                nums2[j] if j != n else float("inf")
            ):
                right = i - 1
            elif (nums2[j - 1] if j != 0 else float("-inf")) > (
                nums1[i] if i != m else float("inf")
            ):
                left = i + 1
            else:
                break
        if (m + n) % 2 == 0:
            return (
                max(
                    nums1[i - 1] if i != 0 else float("-inf"),
                    nums2[j - 1] if j != 0 else float("-inf"),
                )
                + min(
                    nums1[i] if i != m else float("inf"),
                    nums2[j] if j != n else float("inf"),
                )
            ) / 2
        else:
            return max(
                nums1[i - 1] if i != 0 else float("-inf"),
                nums2[j - 1] if j != 0 else float("-inf"),
            )


# 测试用例1: [1,3] + [2] = [1,2,3], 中位数应该是 2.0
nums1 = [1, 3]
nums2 = [2]
solution = Solution()
result = solution.findMedianSortedArrays(nums1, nums2)
print(f"Test 1: {result}, Expected: 2.0")

# 测试用例2: [1,2] + [3,4] = [1,2,3,4], 中位数应该是 2.5
result2 = solution.findMedianSortedArrays([1, 2], [3, 4])
print(f"Test 2: {result2}, Expected: 2.5")

# 测试用例3: [] + [1] = [1], 中位数应该是 1.0
result3 = solution.findMedianSortedArrays([], [1])
print(f"Test 3: {result3}, Expected: 1.0")
